2 * Matt McCutchen's Big Integer Library
5 #ifndef BIGINTEGERUTILS
6 #define BIGINTEGERUTILS
8 #include "BigInteger.hh"
14 * (1) `std::string <=> BigUnsigned/BigInteger' conversion routines easier than `BigUnsignedInABase'
15 * (2) << and >> operators for BigUnsigned/BigInteger, std::istream/std::ostream
18 // Conversion routines. Base 10 only.
19 std::string
easyBUtoString(const BigUnsigned
&x
);
20 std::string
easyBItoString(const BigInteger
&x
);
21 BigUnsigned
easyStringToBU(const std::string
&s
);
22 BigInteger
easyStringToBI(const std::string
&s
);
24 // Creates a BigInteger from data such as `char's; read below for details.
26 BigInteger
easyDataToBI(const T
* data
, BigInteger::Index length
, BigInteger::Sign sign
);
28 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
29 std::ostream
&operator <<(std::ostream
&os
, const BigUnsigned
&x
);
31 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
32 // My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF).
33 std::ostream
&operator <<(std::ostream
&os
, const BigInteger
&x
);
36 * =================================
37 * BELOW THIS POINT are template definitions; above are declarations. See `NumberlikeArray.hh'.
41 * Converts binary data to a BigInteger.
42 * Pass an array `data', its length, and the desired sign.
44 * Elements of `data' may be of any type `T' that has the following
45 * two properties (this includes almost all integral types):
47 * (1) `sizeof(T)' correctly gives the amount of binary data in one
48 * value of `T' and is a factor of `sizeof(Blk)'.
50 * (2) When a value of `T' is casted to a `Blk', the low bytes of
51 * the result contain the desired binary data.
54 BigInteger
easyDataToBI(const T
* data
, BigInteger::Index length
, BigInteger::Sign sign
) {
55 // really ceiling(numBytes / sizeof(BigInteger::Blk))
56 unsigned int pieceSizeInBits
= 8 * sizeof(T
);
57 unsigned int piecesPerBlock
= sizeof(BigInteger::Blk
) / sizeof(T
);
58 unsigned int numBlocks
= (length
+ piecesPerBlock
- 1) / piecesPerBlock
;
60 // Allocate our block array
61 BigInteger::Blk
*blocks
= new BigInteger::Blk
[numBlocks
];
63 BigInteger::Index blockNum
, pieceNum
, pieceNumHere
;
66 for (blockNum
= 0, pieceNum
= 0; blockNum
< numBlocks
; blockNum
++) {
67 BigInteger::Blk curBlock
= 0;
68 for (pieceNumHere
= 0; pieceNumHere
< piecesPerBlock
&& pieceNum
< length
;
69 pieceNumHere
++, pieceNum
++)
70 curBlock
|= (BigInteger::Blk(data
[pieceNum
]) << (pieceSizeInBits
* pieceNumHere
));
71 blocks
[blockNum
] = curBlock
;
74 // Create the BigInteger.
75 BigInteger
x(blocks
, numBlocks
, sign
);